package top.jiangqiang.qianyi.common.storage.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.jiangqiang.qianyi.common.id.IDGenerator;
import top.jiangqiang.qianyi.common.properties.CacheProperties;
import top.jiangqiang.qianyi.common.properties.FileProperties;
import top.jiangqiang.qianyi.common.properties.StorageProperties;
import top.jiangqiang.qianyi.common.storage.StorageService;
import top.jiangqiang.qianyi.common.storage.impl.LocalStorageService;
import top.jiangqiang.qianyi.common.storage.impl.S3StorageService;

import java.util.List;
import java.util.Optional;

/**
 * @Date 2024/7/5 下午4:38
 * @Author Jiangqiang
 */
@Configuration
@Slf4j
@RequiredArgsConstructor
public class StorageAutoConfiguration {
    private final IDGenerator idGenerator;

    private static StorageService getStorageService(IDGenerator idGenerator, StorageProperties storageProperties, CacheProperties cacheProperties) {
        String active = storageProperties.getActive();
        List<LocalStorageConfig> localStorageConfigs = storageProperties.getLocalStorageConfigs();
        List<S3StorageConfig> s3StorageConfigs = storageProperties.getS3StorageConfigs();
        if (StrUtil.isNotBlank(active)) {
            if (CollUtil.isNotEmpty(s3StorageConfigs)) {
                Optional<S3StorageConfig> first = s3StorageConfigs.stream().filter(storageConfig -> active.equals(storageConfig.getName())).findFirst();
                if (first.isPresent()) {
                    S3StorageConfig s3StorageConfig = first.get();
                    return new S3StorageService(idGenerator, s3StorageConfig, cacheProperties.getCachePath());
                }
            }
            if (CollUtil.isNotEmpty(localStorageConfigs)) {
                Optional<LocalStorageConfig> first = localStorageConfigs.stream().filter(storageConfig -> active.equals(storageConfig.getName())).findFirst();
                if (first.isPresent()) {
                    return new LocalStorageService(idGenerator, first.get(), cacheProperties.getCachePath());
                }
            }
        }
        return null;
    }

    @Bean
    StorageService storageService(StorageProperties storageProperties, FileProperties fileProperties, CacheProperties cacheProperties) {
        StorageService storageService = getStorageService(idGenerator, storageProperties, cacheProperties);
        if (storageService == null) {
            log.info("存储器没有配置或配置不完整，将使用默认的本地存储器，上传路径{}", fileProperties.getUploadPath());
            return getDefaultStorageService(fileProperties, cacheProperties);
        }
        if (!storageService.test()) {
            log.warn("配置的存储器无法正常使用，将使用默认的本地存储器，上传路径{}", fileProperties.getUploadPath());
            return getDefaultStorageService(fileProperties, cacheProperties);
        }
        return storageService;
    }

    private StorageService getDefaultStorageService(FileProperties fileProperties, CacheProperties cacheProperties) {
        LocalStorageConfig localStorageConfig = new LocalStorageConfig();
        localStorageConfig.setUploadPath(fileProperties.getUploadPath());
        log.warn("默认的存储器配置由于没有域名或者协访问议相关配置，所以只应该用于开发测试用途，切勿用于正式环境，否则生产的访问路径无法正常使用");
        return new LocalStorageService(idGenerator, localStorageConfig, cacheProperties.getCachePath());
    }
}
